// MyFamily - XML based genealogy program.
// Copyright (C) 2006  Doug Swisher
// See the file COPYING.txt for license details.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

using DataModel;
using CommonGui;
using WebLib;


namespace MyFamily4
{
    public partial class MyFamilyForm : Form
    {
        private WebSiteSettings webSiteSettings = new WebSiteSettings();

        public MyFamilyForm()
        {
            InitializeComponent();
        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void GoButton_Click(object sender, EventArgs e)
        {
            try
            {
                // Try to open the input file...
                FileInfo inputInfo = new FileInfo(InputFileTextBox.Text);
                if (!inputInfo.Exists)
                {
                    MessageBox.Show(this, "Could not find input file!", "Boom!");
                    return;
                }

                // Make sure they really wanna overwrite the output file, if it exists...
                DirectoryInfo outputInfo = new DirectoryInfo(WebSiteOutputTextBox.Text);
                if (outputInfo.Exists)
                {
                    if (outputInfo.GetFiles("*.htm").Length > 0)
                    {
                        if (MessageBox.Show(this, "Output directory already contains a web site, do you wish to overwrite it?", "Overwrite web site?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            // TODO - Doug - 8/27/2006 - clean out ALL the files in the directory, not just the .htm files...
                            foreach (FileInfo file in outputInfo.GetFiles("*.htm"))
                            {
                                file.Delete();
                            }
                        }
                        else
                        {
                            return;
                        }
                    }
                }
                else
                {
                    if (MessageBox.Show(this, "Output directory does not exist.  Do you wish to create it?", "Create Directory?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        outputInfo.Create();
                    }
                    else
                    {
                        return;
                    }
                }

                // Give 'em a pretty cursor...the caller will reset back to the default.
                this.Cursor = Cursors.WaitCursor;

                // Create the results object...
                LoadResults loadResults = new LoadResults(inputInfo);

                // Read in the info...
                FamilyInfo family = FamilyInfo.Load(inputInfo, loadResults);

                // Set up the web site results...
                WebSiteResults webSiteResults = new WebSiteResults(outputInfo);

                // Generate the web site...
                WebSite web = new WebSite();

                web.Generate(outputInfo, family, this.webSiteSettings, webSiteResults);

                LoadResultsDialog2 loadResultsDialog = new LoadResultsDialog2(loadResults, webSiteResults);
                loadResultsDialog.ShowDialog(this);

            }
            // TODO - Doug - 8/27/2006 - Put this back in place...for now, it's ok for these to bubble out...
            //catch (Exception e)
            //{
            //    MessageBox.Show(this, e.ToString(), "Boom!");
            //}
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void WebSiteSettingsButton_Click(object sender, EventArgs e)
        {
            WebSiteSettingsDialog dialog = new WebSiteSettingsDialog();

            dialog.Settings = this.webSiteSettings.Clone();

            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                this.webSiteSettings = dialog.Settings;
            }
        }

        private void InputBrowseButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Xml Files (*.xml)|*.xml|All files (*.*)|*.*";
            dialog.DefaultExt = "xml";

            if (!string.IsNullOrEmpty(InputFileTextBox.Text))
            {
                FileInfo info = new FileInfo(InputFileTextBox.Text);
                if ((info.Directory != null) && (info.Directory.Exists))
                {
                    dialog.InitialDirectory = info.Directory.FullName;
                }
            }

            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                InputFileTextBox.Text = dialog.FileName;
            }
        }

    }
}